﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Workflow.Runtime;
using System.Workflow.Activities;

namespace BneyBaruch.Ingest.Core.WorkflowCommunication.Extensions
{
    public class WorkflowRuntimePerServiceContainer : IExtension<ServiceHostBase>, IDisposable
    {
        #region Fields

        private WorkflowRuntime workflowRuntime;
        private string workflowServicesConfig;

        #endregion

        #region Properties

        public WorkflowRuntime WorkflowRuntime
        {
            get { return workflowRuntime; }
        }

        #endregion

        #region Constructors

        public WorkflowRuntimePerServiceContainer(string workflowServicesConfig)
        {
            this.workflowServicesConfig = workflowServicesConfig;
        }

        #endregion

        #region IExtension<ServiceHostBase> Members

        /// <summary>
        /// Enables an extension object to find out when it has been aggregated. 
        /// Called when the extension is added to the <see cref="P:System.ServiceModel.IExtensibleObject`1.Extensions"/> property.
        /// </summary>
        /// <param name="owner">The extensible object that aggregates this extension.</param>
        public void Attach(ServiceHostBase owner)
        {
            // When this Extension is attached within the Service Host, create a
            // new instance of the WorkflowServiceContainer
            workflowRuntime = new WorkflowRuntime(workflowServicesConfig);
            workflowRuntime.ServicesExceptionNotHandled +=
                new EventHandler<ServicesExceptionNotHandledEventArgs>(
                    workflowRuntime_ServicesExceptionNotHandled);

            ExternalDataExchangeService exSvc = new ExternalDataExchangeService();
            workflowRuntime.AddService(exSvc);

            // Start the services associated with the container
            workflowRuntime.StartRuntime();
        }

        /// <summary>
        /// Enables an object to find out when it is no longer aggregated. 
        /// Called when an extension is removed from the <see cref="P:System.ServiceModel.IExtensibleObject`1.Extensions"/> property.
        /// </summary>
        /// <param name="owner">The extensible object that aggregates this extension.</param>
        public void Detach(ServiceHostBase owner)
        {
            // When this WCF Extension is detached, then just stop the Workflow Container
            workflowRuntime.StopRuntime();
        }

        #endregion

        #region Private Methods

        private void workflowRuntime_ServicesExceptionNotHandled(object sender, ServicesExceptionNotHandledEventArgs e)
        {
            Console.WriteLine("ServicesExceptionNotHandled: " + e.Exception.ToString());
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            workflowRuntime.Dispose();
        }

        #endregion
    }
}
